package com.mobile.check.present;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Workbook;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NLog;
import com.mobile.check.activitys.TaskItemCheckActivity;
import com.mobile.check.beans.TaskItemInfo;
import com.mobile.check.db.EntityManager;
import com.mobile.check.dialogs.BottomItem;
import com.mobile.check.util.DoubleUtil;

/**
 * 类名: TaskItemDetailPresent
 * 作者: 李学伟
 * 日期: 2020年10月13日 22:34
 * 描述:
 */
public class TaskItemCheckPresent extends BasePresent<TaskItemCheckActivity> {
    List<TaskItemInfo> list;

    public void getTaskItemInfos(Long parentDbId, int index) {
        if (Kits.Empty.check(list)) {
            list = EntityManager.getInstance().queryTaskItemInfoByParentId(parentDbId);
            Iterator<TaskItemInfo> iterator = list.iterator();
            List<String> labelList = new ArrayList<>();
            while (iterator.hasNext()) {
                TaskItemInfo itemInfo = iterator.next();
                String equment = itemInfo.getEquipment();
                String label = itemInfo.getLabel();
                String area = itemInfo.getArea();
                String key = area + equment + label;
                if (labelList.contains(key)) {
                    iterator.remove();
                } else {
                    labelList.add(key);
                }
            }
        }
        getV().getTaskItemDetialSuccess(list.get(index));
    }

    public int getTaskItemInfoSize(Long parentDbId) {
        if (Kits.Empty.check(list)) {
            list = EntityManager.getInstance().queryTaskItemInfoByParentId(parentDbId);
            Iterator<TaskItemInfo> iterator = list.iterator();
            List<String> labelList = new ArrayList<>();
            while (iterator.hasNext()) {
                TaskItemInfo itemInfo = iterator.next();
                String equment = itemInfo.getEquipment();
                String label = itemInfo.getLabel();
                String area = itemInfo.getArea();
                String key = area + equment + label;

                if (labelList.contains(key)) {
                    iterator.remove();
                } else {
                    labelList.add(key);
                }
            }
        }
        return list.size();
    }


    private Map<Integer, Object> getFillExcelMap(TaskItemInfo itemInfo, int taskType) throws Exception {
        Map<Integer, Object> valueMap = new HashMap<>();
        Double testValue;
        Double backValue;
        Double realValue;
        SimpleDateFormat ymdhms2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        switch (taskType) {
            case 0:
                try {
                    Date date = ymdhms2.parse(itemInfo.getTestDate());
                    valueMap.put(9, date);
                } catch (ParseException e) {
                    e.printStackTrace();
                    valueMap.put(9, itemInfo.getTestDate());
                }

                valueMap.put(10, itemInfo.getTestDevice());
                testValue = DoubleUtil.getDouble(itemInfo.getTestValue(), 1);
                backValue = DoubleUtil.getDouble(itemInfo.getBackGround(), 1);

//                realValue = testValue - backValue;
                realValue = testValue + backValue;//检测值最终结果为 实际检测值 + 背景值
                realValue = DoubleUtil.getDouble(realValue, 1);
                if (realValue < 0) {
                    realValue = 0d;
                }
                valueMap.put(11, realValue);
                if (itemInfo.getIsLeak().equals("1")) {
                    valueMap.put(12, Double.valueOf("1"));
                    valueMap.put(13, itemInfo.getLeakLocation());
                } else {
                    valueMap.put(12, Double.valueOf("0"));
                    valueMap.put(13, "");
                }


                valueMap.put(14, backValue);
                valueMap.put(17, 0);
                valueMap.put(18, itemInfo.getTestPerson());
                break;
            case 1:


                try {
                    Date date = ymdhms2.parse(itemInfo.getRepeatDate());
                    valueMap.put(15, date);
                } catch (ParseException e) {
                    e.printStackTrace();
                    valueMap.put(15, itemInfo.getRepeatDate());
                }


                valueMap.put(16, itemInfo.getRepeatDevice());

                testValue = DoubleUtil.getDouble(itemInfo.getRepeatValue(), 1);
                backValue = DoubleUtil.getDouble(itemInfo.getBackGround(), 1);

                realValue = testValue + backValue;
                if (realValue <= 0) {
                    realValue = 0d;
                }
                realValue = DoubleUtil.getDouble(realValue, 1);
                valueMap.put(17, realValue);
//                valueMap.put(17, itemInfo.getRepeatValue());
                valueMap.put(18, backValue);
                valueMap.put(21, itemInfo.getRepeatPerson());
                break;
            case 2:

                try {
                    Date date = ymdhms2.parse(itemInfo.getTestDate());
                    valueMap.put(10, date);
                } catch (ParseException e) {
                    e.printStackTrace();
                    valueMap.put(10, itemInfo.getTestDate());
                }
                valueMap.put(11, itemInfo.getTestDevice());
                testValue = DoubleUtil.getDouble(itemInfo.getTestValue(), 1);
                backValue = DoubleUtil.getDouble(itemInfo.getBackGround(), 1);

                realValue = testValue + backValue;
//                realValue = testValue;
                if (realValue <= 0) {
                    realValue = 0d;
                }
                realValue = DoubleUtil.getDouble(realValue, 1);
                valueMap.put(12, realValue);
//                valueMap.put(12, itemInfo.getTestValue());
                valueMap.put(15, backValue);
                valueMap.put(16, itemInfo.getTestPerson());
                break;
        }
        return valueMap;
    }


    public List<BottomItem> getLeakLocation() {
        final String[] cities = {"阀杆", "阀芯", "法兰", "丝扣", "开口管线", "管帽", "丝堵", "活接", "空冷丝堵", "轴封", "安全阀", "阀盖法兰"};
        List<BottomItem> list = new ArrayList<>();
        for (String name : cities) {
            list.add(new BottomItem(name, name));
        }
        return list;
    }

    public void onDestory() {

    }

    public float getFloat(String value) {
        try {
            return Float.parseFloat(value);
        } catch (Exception ex) {
            return 0;
        }
    }

    public float getDouble(String value, int scal) {
        try {
            return Float.parseFloat(value);
        } catch (Exception ex) {
            return 0;
        }
    }

    private void setCell(Workbook workbook, Cell cell, Object value) {

        CellStyle cellStyle = cell.getCellStyle();
        int cellType = cell.getCellType();
        NLog.d("setCell", "cellStyle=" + cellStyle);
        NLog.d("setCell", "cellType=" + cellType);
        if (null == cellStyle) {
            cellStyle = workbook.createCellStyle();
            cell.setCellStyle(cellStyle);
        }
        String fmt = cellStyle.getDataFormatString();
        NLog.d("setCell", "fmt=" + fmt);
        DataFormat format = workbook.createDataFormat();
        if (value instanceof String) {
            NLog.d("setCell", "String-" + value);
            cell.setCellValue(String.valueOf(value));
        } else if (value instanceof Integer) {
            cell.setCellValue((int) value);
        } else if (value instanceof Double) {
            cellStyle.setDataFormat((short) 0);
            cell.setCellValue((double) value);
        } else if (value instanceof Boolean) {
            NLog.d("setCell", "Boolean-" + value);
            cell.setCellValue((boolean) value);
        } else if (value instanceof Date) {
            CellStyle dateStyle = workbook.createCellStyle();
            dateStyle.cloneStyleFrom(cellStyle);
            dateStyle.setDataFormat(format.getFormat("yyyy/M/dd HH:mm:ss"));
            NLog.d("setCell", "Date-" + value);
            cellStyle = dateStyle;
            cell.setCellValue((Date) value);
        } else {
            cell.setCellValue(value.toString());
        }
        cell.setCellStyle(cellStyle);
    }

    public void save3(final TaskItemInfo itemInfo, final int taskType, final int type) {
        EntityManager.getInstance().saveTaskItemInfo(itemInfo);
        getV().saveSuccess(type);
    }


}